unit uQuery;

interface

uses Uni, DB;

type
  TMyQuery = class(TUniQuery)

    {$REGION '<< Private >>'}

      {$REGION '<< Methods >>'}

        procedure SetConnection();

      {$ENDREGION}

    {$ENDREGION}

    {$REGION '<< Public >>'}

      public

        {$REGION '<< Methods >>'}

          constructor Create(); virtual;

          function GetNextID(const a_Sequence: String): Int64;

          function GetFieldValue(const a_FieldName: String): Variant;

          procedure SelectAll(const a_Campos: String; const a_TableName: String; const a_OrderBy: String = ''; const a_Open: Boolean = True);
          procedure SelectWhere(const a_Campos: String; const a_TableName: String; const a_CondictionWhere: String; const a_OrderBy: String = ''; const a_Open: Boolean = True);

          procedure DeleteAll(const a_TableName: String);
          procedure DeleteWhere(const a_TableName: String; const a_CondictionWhere: String);

          procedure Insert(const a_TableName: String; const a_Columns: String);

          procedure NewParameter(const a_Param: String; const a_FieldType: TFieldType);
          procedure SetParameterValue(const a_Param: String; const a_FieldType: TFieldType; const a_Value: Variant);

          procedure ExecSQL(); virtual;
          procedure Open(); virtual;

        {$ENDREGION}

    {$ENDREGION}

  end;

implementation

{$REGION '<< TMyQuery >>}

uses uConexao, SysUtils, uMensagens, Windows, Forms, Controls, Variants;

constructor TMyQuery.Create;
begin
  inherited Create(nil);
  Self.SetConnection();
end;

procedure TMyQuery.DeleteAll(const a_TableName: String);
begin
  Self.Close();

  Self.SQL.Add('DELETE FROM');
  Self.SQL.Add(       a_TableName);

  Self.ExecSQL();
end;

procedure TMyQuery.DeleteWhere(const a_TableName, a_CondictionWhere: String);
begin
  Self.Close();

  Self.SQL.Add('DELETE FROM');
  Self.SQL.Add(       a_TableName);
  Self.SQL.Add('WHERE');
  Self.SQL.Add(       a_CondictionWhere);

  Self.ExecSQL();
end;

procedure TMyQuery.ExecSQL;
begin
  try
    Screen.Cursor := crSQLWait;
    try
      inherited ExecSQL();
    except on E: Exception
      do TDialog.Send('Ocorreu um erro ao executar a consulta: ' + E.Message, MB_OK + MB_ICONERROR);
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

function TMyQuery.GetFieldValue(const a_FieldName: String): Variant;
begin
  case Self.FieldByName(a_FieldName).DataType of

    ftString: Result := Self.FieldByName(a_FieldName).AsString;

    ftInteger: Result := Self.FieldByName(a_FieldName).AsInteger;

    ftLargeint: Result := Self.FieldByName(a_FieldName).AsLargeInt;

    ftDate,
    ftDateTime: Result := Self.FieldByName(a_FieldName).AsDateTime;

    ftBoolean: Result := Self.FieldByName(a_FieldName).AsBoolean;

  end;
end;

function TMyQuery.GetNextID(const a_Sequence: String): Int64;
begin
  Self.Close();

  Self.SQL.Add('SELECT GEN_ID(' + a_Sequence + ', 1) AS SEQ FROM RDB$DATABASE');

  Self.Open();

  Result := Self.FieldByName('SEQ').AsLargeInt;
end;

procedure TMyQuery.Insert(const a_TableName, a_Columns: String);
var
  Indice: Integer;
  Values: String;
begin
  try
    Screen.Cursor := crSQLWait;

    for Indice := 0 to Self.Params.Count - 1 do
      begin
        if Indice = 0
          then Values := VarToStr(Self.Params.Items[Indice].Value)
          else Values := Values + ',' + VarToStr(Self.Params.Items[Indice].Value);
      end;

    Self.SQL.Add('INSERT INTO');
    Self.SQL.Add(     a_TableName + '(' + a_Columns + ')');
    Self.SQL.Add('VALUES');
    Self.SQL.Add(     '(' + Values + ')');

    Self.ExecSQL();
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TMyQuery.NewParameter(const a_Param: String; const a_FieldType: TFieldType);
begin
  Self.Params.CreateParam(a_FieldType, a_Param, ptInput);
end;

procedure TMyQuery.Open;
begin
  try
    Screen.Cursor := crSQLWait;
    try
      inherited Open();
    except on E: Exception
      do TDialog.Send('Ocorreu um erro ao abrir a consulta: ' + E.Message, MB_OK + MB_ICONERROR);
    end;
  finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TMyQuery.SelectAll(const a_Campos: String; const a_TableName, a_OrderBy: String; const a_Open: Boolean);
begin
  Self.Close();

  Self.SQL.Add('SELECT');
  Self.SQL.Add(       a_Campos);
  Self.SQL.Add('FROM');
  Self.SQL.Add(       a_TableName);

  if not (a_OrderBy = '') then
    begin
      Self.SQL.Add('ORDER BY');
      Self.SQL.Add(       a_OrderBy);
    end;

  if a_Open
    then Self.Open();
end;

procedure TMyQuery.SelectWhere(const a_Campos: String; const a_TableName, a_CondictionWhere, a_OrderBy: String; const a_Open: Boolean);
begin
  Self.Close();

  Self.SQL.Add('SELECT');
  Self.SQL.Add(       a_Campos);
  Self.SQL.Add('FROM');
  Self.SQL.Add(       a_TableName);
  Self.SQL.Add('WHERE');
  Self.SQL.Add(       a_CondictionWhere);

  if not (a_OrderBy = '') then
    begin
      Self.SQL.Add('ORDER BY');
      Self.SQL.Add(       a_OrderBy);
    end;

  if a_Open
    then Self.Open();
end;

procedure TMyQuery.SetConnection;
begin
  Self.Connection := TConexao.GetInstance().UniConnection;
end;

procedure TMyQuery.SetParameterValue(const a_Param: String; const a_FieldType: TFieldType; const a_Value: Variant);
begin
  if not Assigned(Self.Params.FindParam(a_Param))
    then Self.NewParameter(a_Param, a_FieldType);

  case a_FieldType of
    ftString: Self.ParamByName(a_Param).AsString := QuotedStr(a_Value);
  end;
end;

{$ENDREGION}

end.
